package cn.sfrog.project.biz.web.controller;

import cn.sfrog.common.constant.ConfigConstants;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.UserTypeEnum;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.service.IBizUserEnsureMoneyService;
import cn.sfrog.project.biz.admin.service.IBizUserTradeRecordService;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordQueryVO;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordResultVO;
import cn.sfrog.project.biz.web.vo.money.AppNeedPayMoneyQueryVO;
import cn.sfrog.project.biz.web.vo.money.AppNeedPayMoneyResultVO;
import cn.sfrog.project.biz.web.vo.money.AppPaymentEarnestMoneyFormVO;
import cn.sfrog.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/api/earnestMoney")
@Api(value = "/api/earnestMoney", tags = "APP保证金相关")
public class AppEarnestMoneyController extends BaseController {

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IBizUserEnsureMoneyService bizUserEnsureMoneyService;
    @Autowired
    private IBizUserTradeRecordService bizUserTradeRecordService;




    @ApiOperation("获取需要缴纳的保证金金额")
    @GetMapping("/getNeedPayMoney")
    public ApiAjaxResult<AppNeedPayMoneyResultVO> getNeedPayMoney(AppNeedPayMoneyQueryVO query){
        AppNeedPayMoneyResultVO resultVO = new AppNeedPayMoneyResultVO();
        String key = "";
        if(query.getUserType().equals(UserTypeEnum.ESCORT.getCode())){
            //陪诊
            key = ConfigConstants.ESCORT_SURE_MONEY;
        }else if(query.getUserType().equals(UserTypeEnum.CARE_WORKER.getCode())){
            //护工
            key = ConfigConstants.CAREWORKER_SURE_MONEY;
        }else if(query.getUserType().equals(UserTypeEnum.CARE_NURSE.getCode())){
            //护工
            //todo 暂时已护工的保证金为准
            key = ConfigConstants.CAREWORKER_SURE_MONEY;
        }else if(query.getUserType().equals(UserTypeEnum.DOCTOR.getCode())){
            //医生
            key = ConfigConstants.DOCTOR_SURE_MONEY;
        }
        resultVO.setEarnestMoney(new BigDecimal(sysConfigService.selectConfigByKey(key)));
        return ApiAjaxResult.success(resultVO);
    }

    @ApiOperation("交保证金")
    @PostMapping("/paymentEarnestMoney")
    public  ApiAjaxResult paymentEarnestMoney(@RequestBody @Validated AppPaymentEarnestMoneyFormVO form){
        Map<String, String> map =  bizUserEnsureMoneyService.paymentEarnestMoney(form);
        return ApiAjaxResult.success(map);
    }

    @ApiOperation("申请保证金退费")
    @PostMapping("/applyReturnMoney")
    public ApiAjaxResult applyReturnMoney(){
       Boolean flag =  bizUserEnsureMoneyService.applyReturnMoney();
        return ApiAjaxResult.success(flag);
    }

    @ApiOperation("获取保证金交易数据列表")
    @GetMapping("/applyReturnMoney")
    public TableDataInfo<List<AppUserTradeRecordResultVO>> getEarnestMoneyList(AppUserTradeRecordQueryVO queryVO){
        queryVO.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<AppUserTradeRecordResultVO> resultVOList = bizUserTradeRecordService.selectAppEarnestMoneyList(queryVO);
        return getDataTable(resultVOList);
    }

}
